/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package py.com.spadons.esmetil.servlet.listener;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import py.com.spadons.esmetil.entities.Dato;
import py.com.spadons.esmetil.entities.Estacion;
import py.com.spadons.esmetil.entities.EstacionDetalle;
import py.com.spadons.esmetil.entities.Magnitud;
import py.com.spadons.esmetil.entities.Unidad;

/**
 *
 * @author spadons
 */
@WebServlet(name = "listener", urlPatterns = {"/listener"})
public class listener extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @EJB
    private py.com.spadons.esmetil.sessionBeans.DatoFacade datoFacade;
    private String data;
    private String dataEstacion;
    @EJB
    private py.com.spadons.esmetil.sessionBeans.EstacionFacade estacionFacade;
    private Estacion estacion;
    private String dataEstacionDetalle;
    @EJB
    private py.com.spadons.esmetil.sessionBeans.EstacionDetalleFacade estacionDetalleFacade;
    private EstacionDetalle estacionDetalle;
    private String dataMagnitud;
    @EJB
    private py.com.spadons.esmetil.sessionBeans.MagnitudFacade magnitudFacade;
    private Magnitud magnitud;
    private String dataUnidad;
    @EJB
    private py.com.spadons.esmetil.sessionBeans.UnidadFacade unidadFacade;
    private Unidad unidad;
    private String dataHora;
    private String dataMinuto;
    private String dataSegundo;
    private String dataDia;
    private String dataMes;
    private String dataAno;
    private String dataFechaHora;
    private Date date;
    private String dataValor;
    private double valor;
    private String version;
    private String dataParametro = "data";
    private String resultado = "ok";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {

            data = request.getParameter(dataParametro);
            version = data.substring(0, 8);

            if (version.equals("00000.00")) {

                dataEstacion = data.substring(8, 14);
                estacion = estacionFacade.find(Integer.parseInt(dataEstacion));

                dataEstacionDetalle = data.substring(14, 23);
                estacionDetalle = estacionDetalleFacade.find(Long.parseLong(dataEstacionDetalle));

                dataMagnitud = data.substring(23, 29);
                //magnitud = magnitudFacade.find(Integer.parseInt(dataMagnitud));
                magnitud = estacionDetalle.getMagnitud();
                
                dataUnidad = data.substring(29, 35);
                //unidad = unidadFacade.find(Integer.parseInt(dataUnidad));
                unidad = estacionDetalle.getUnidadMedida();
                
                dataFechaHora = data.substring(35, 49);
                
                /*
                dataHora = data.substring(35, 37);
                dataMinuto = data.substring(37, 39);
                dataSegundo = data.substring(39, 41);
                dataDia = data.substring(41, 43);
                dataMes = data.substring(43, 45);
                dataAno = data.substring(45, 49);
                */
                
                date = (new SimpleDateFormat("yyyyMMddHHmmss")).parse(dataFechaHora);

                dataValor = data.substring(49, 61);
                valor = Double.parseDouble(dataValor);
                
                Dato dato = new Dato();
                
                dato.setEstacion(estacion);
                dato.setEstacionDetalle(estacionDetalle);
                dato.setMagnitud(magnitud);
                dato.setUnidad(unidad);
                dato.setMomento(date);
                dato.setValor(valor);
                
                datoFacade.create(dato);
                
            }

        } catch (Exception e) {
            resultado = e.getMessage();
        }

        response.setContentType(
                "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();


        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Esmetil Listener</title>");
            out.println("</head>");
            out.println("<body>");
            out.println(resultado);
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
